<?php
// $Header: /cvsroot/tsheet/timesheet.php/task_maint.php,v 1.11 2005/05/17 03:38:37 vexil Exp $
// Authenticate
require("class.AuthenticationManager.php");
require("class.CommandMenu.php");
if (!$authenticationManager->isLoggedIn() || !$authenticationManager->hasClearance(CLEARANCE_ADMINISTRATOR)) {
	Header("Location: login.php?redirect=$_SERVER[PHP_SELF]&clearanceRequired=Administrator");
	exit;
}

// Connect to database.
$dbh         = dbConnect();
$contextUser = strtolower($_SESSION['contextUser']);

// Define the command menu
include("timesheet_menu.inc");

if (empty($proj_id))
{
	$proj_id = 1;
}	
// Make sure the selected project is valid for this client
if ($client_id != 0) {
	if (!isValidProjectForClient($proj_id, $client_id))
		$proj_id = getValidProjectForClient($client_id);
}

// Set up the required queries
$query_task    = "select   distinct $TASK_TABLE.task_id, $TASK_TABLE.name, $TASK_TABLE.identifier, $TASK_TABLE.description, $TASK_TABLE.source_project, $TASK_TABLE.target_project, $TASK_TABLE.status, $TASK_TABLE.manager, $TASK_TABLE.cir, $PROJECT_TABLE.title, $PROJECT_TABLE.proj_id, ".
                           "DATE_FORMAT(assigned , '%M %d, %Y') as assigned ,".
                           "DATE_FORMAT(started  , '%M %d, %Y') as started  ,".
                           "DATE_FORMAT(suspended, '%M %d, %Y') as suspended,".
                           "DATE_FORMAT(completed, '%M %d, %Y') as completed ".
                 "from     $TASK_TABLE, $PROJECT_TABLE ".
                 "where    $PROJECT_TABLE.proj_id = $proj_id AND $TASK_TABLE.source_project = $PROJECT_TABLE.title ".
                 "order by $TASK_TABLE.task_id";

$query_project = "select  distinct title, description,".
                          "DATE_FORMAT(start_date, '%M %d, %Y') as start_date,".
                          "DATE_FORMAT(deadline, '%M %d, %Y') as deadline,".
                          "proj_status, proj_leader ".
                  "from   $PROJECT_TABLE ".
                  "where  $PROJECT_TABLE.proj_id = $proj_id";
?>

<html>
<head>
	<title>Tasks</title>
<?php
include ("header.inc");
?>
<script language="Javascript">

	function delete_task(projectId, taskId) {
				if (confirm('Deleting a task which has been used in the past will make those timesheet ' +
												'entries invalid, and may cause errors. This action is not recommended. ' + 
												'Are you sure you want to delete this task?'))
					location.href = 'task_action.php?proj_id=' + projectId + '&task_id=' + taskId + '&action=delete';
	}

</script>
</head>
<body <?php include ("body.inc"); ?> >
<?php include ("banner.inc");
?>

<form name="changeForm" action="<?php echo $_SERVER["PHP_SELF"]; ?>" style="margin-bottom: 0px;">										

<table width="100%" border="0" cellspacing="0" cellpadding="0">
	<tr>
		<td width="100%" class="face_padding_cell">
			<table width="100%" border="0">
				<tr>
					<td align="left" nowrap>
						<table width="100%" height="100%" border="0" cellpadding="1" cellspacing="2">
							<tr>
								<td>
									<table width="100%" border="0" cellspacing="0" cellpadding="0">
										<tr>
											<td><table width="50"><tr><td>Client:</td></tr></table></td>
											<td width="100%"><?php client_select_list($client_id, 0, false, false, true, false, "submit();", false); ?></td>
										</tr>
										<tr>
											<td height="1"></td>
											<td height="1"><img src="images/spacer.gif" width="150" height="1" /></td>
										</tr>																						
									</table>
								</td>	
								<td>		
									<table width="100%" border="0" cellspacing="0" cellpadding="0">
										<tr>
											<td><table width="50"><tr><td>Project:</td></tr></table></td>
											<td width="100%"><?php project_select_list($client_id, false, $proj_id, 0, false, false, "submit();", false); ?></td>
										</tr>
										<tr>
											<td height="1"></td>
											<td height="1"><img src="images/spacer.gif" width="150" height="1" /></td>
										</tr>
									</table>
								</td>
							</tr> 
						</table>
					</td>
					<td align="center" class="outer_table_heading" nowrap>
						Tasks</td>
					</td>
					<td align="right" nowrap>
						<?php if ($proj_id != 0) { ?>
						<a href="task_add.php?proj_id=<?php echo $proj_id; ?>">Add new task</a>
						<?php } else { ?>
							<span class="disabledLink">Add new task</span>
						<?php } ?>
					</td>
				</tr>
			</table> 
			<table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" class="outer_table">
				<tr>
					<td>					
						<table width="100%" border="0" cellspacing="0" cellpadding="0" class="table_body">					
                            <?php  
                            	// Execute query
                            	list($qh_task, $num_task) = dbQuery($query_task);
                            	
                            	// Were there any results
                            	if ($num_task == 0) {    
                            		if ($proj_id == 0) {
                            			print "	<tr>\n";
                            			print "		<td align=\"center\">\n";
                            			print "			<i><br>Please select a client with projects, or 'All Clients'.<br><br></i>\n";
                            			print "		</td>\n";
                            			print "	</tr>\n";		
                            		}
                            		else {
                            			print "	<tr>\n";
                            			print "		<td align=\"center\">\n";
                            			print "			<i><br>There are no tasks for this project.<br><br></i>\n";
                            			print "		</td>\n";
                            			print "	</tr>\n";
                            		}
                            	}
                            	else 
								{		
                            		//iterate through tasks
                            		for ($j=0; $j<$num_task; $j++) 
									{
                            			$data_task = dbResult($qh_task);
                            			//start the row
                                        ?>
            		                    <tr>
            		                    	<td>
            		                    		<table width="100%" border="0"<?php if ($j+1<$num_task) print "class=\"section_body\""; ?>>
            		                    			<tr>
            		                    				<td valign="center">
            		                    					<span class="project_title"><?php echo stripslashes($data_task["name"]); ?></span>
            		                    					&nbsp;<span class="project_status">&lt;<?php echo $data_task["status"]; ?>&gt;</span><br>
            		                    						<?php echo stripslashes($data_task["description"]); ?>
            		                    				</td>
            		                    				<td align="right" valign="top" nowrap>
            		                    					<span class="label">Actions:</span>
            		                    					<a href="task_edit.php?task_id=<?php echo $data_task["task_id"]; ?>">Edit</a>,
            		                    					<a href="javascript:delete_task(<?php echo $proj_id; ?>,<?php echo $data_task["task_id"]; ?>);">Delete</a>
            		                    				</td>
            		                    			</tr>
            		                    			<tr>
            		                    				<td align="left" colspan="2" align="top">
    						            					<span class="label">Identifier:</span>
							            					<?php echo $data_task["identifier"]; ?><br>
							            				    <span class="label">Source Project:</span>
							            					<?php echo $data_task["source_project"]; ?><br>
							            					<span class="label">target Project:</span>
							            					<?php echo $data_task["target_project"]; ?><br>
    						            					<span class="label">Manager:</span>
							            					<?php echo $data_task["manager"]; ?>
							            					<br>
            		                    					<span class="label">Assigned persons:</span>
                                                            <?php
                                                            // Get assigned users:
                                                            list($qh3, $num_3) = dbQuery("select username, task_id from $TASK_ASSIGNMENTS_TABLE where task_id=$data_task[task_id]");
                                                            if ($num_3 > 0) 
							            					{
                                                            	while ($data_3 = dbResult($qh3)) 
							            						{
                                                            		print "$data_3[username] ";
                                                            	}
                                                            }
                                                            else 
							            					{
                                                            	print "<i>None</i>";
                                                            }
                                                            ?>
							            					<br>
							            					<span class="label">CIR:</span>
							            					<?php echo $data_task["cir"]; ?><br>
                                        				</td>
                                        			<tr>
                                        		</table>
                    		            	</td>
                    		            </tr>						
                                    <?php
                                    } 
                                }
                            ?>
	        			</table>
	        		</td>
	        	</tr>
	        </table>
	

		</td>
	</tr>
</table>

</form>		
<?php include ("footer.inc");
?>
</BODY>
</HTML>

